package com.ludomancy.gamelib;

/*
 * Created on Oct 22, 2003
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class PositionInterpolator 
{
	public int x;
	public int y;
	public int targetx;
	public int targety;
	
	private int dx;
	private int dy;
	
	public PositionInterpolator(int x, int y, int tx, int ty, int steps) 
	{
		this.x = x;
		this.y = y;
		this.targetx = -1;
		this.targety = -1;
		this.dx = 0;
		this.dy = 0;
		moveTo(tx, ty, steps);
	}

	public void moveTo(int tx, int ty, int stepx, int stepy) 
	{	
		this.targetx = tx;
		this.targety = ty;
		this.dx = targetx-x > 0?stepx:-stepx;
		this.dy = targety-y > 0?stepy:-stepy;
	}

	public void moveTo(int tx, int ty, int steps) 
	{
		this.targetx = tx;
		this.targety = ty;
		this.dx = (targetx-x) / steps;
		this.dy = (targety-y) / steps;
	}

	public void setPos(int x, int y) 
	{	
		this.x = x;
		this.y = y;
		this.targetx = x;
		this.targety = y;
		this.dx = 0;
		this.dy = 0;
	}
	
	public void update() 
	{
		if( targetx == -1 || targety == -1) return;
		
		if (x != targetx) 
		{
			x += dx;
			// do not move farther than the target 
			if ((dx > 0 && x > targetx) || (dx < 0 && x < targetx))
				x = targetx; 					
		}
		if (y != targety) 
		{
			y += dy;
			// do not move farther than the target 
			if ((dy > 0 && y > targety) || (dy < 0 && y < targety))
				y = targety; 					
		}
	}
	
	public boolean isMoving() 
	{	
		return (x != targetx || y != targety) && (targetx != -1 && targety != -1);
	}
}